package com.zj.leetcode.hard.stack;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @program: algorithm
 * @description: 基本计算器
 * 224 basic-calculator
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class BasicCalculator {
    public static void main(String[] args) {
//        String s = "(1 +4 + (4 +  5 + (7+5 ) 2)-3)+(6+8) + 9";

//        String s = "4 + (1 +4 + (  5 + (7+5 ) + 2)-3)+(6+18) + 9";
//        String s = "1 + 1";
        String s = " 2-1 + 2 ";
        Solution solution = new BasicCalculator().new Solution();
        System.out.println(solution.calculate(s));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /**
         * "4 + (1 +4 + (  5 + (7+5 ) + 2)-3)+(6+18) + 9";
         * 4 + (5    + (  5 +  12   + 2) -3) + 24  + 9
         *
         * @param s
         * @return
         */
        public int calculate(String s) {
            int size = s.length();
            int symbol = 1;
            int ret = 0;
            Deque<Integer> ops = new LinkedList<Integer>();
            ops.push(1);
            int i = 0;
            while (i < size){

                if (s.charAt(i) == ' ') {
                    i++;
                } else if (s.charAt(i) == '+') {
                    symbol = ops.peek();
                    i++;
                } else if (s.charAt(i) == '-') {
                    symbol = Math.negateExact(ops.peek());
                    i++;
                } else if (s.charAt(i) == '(') {
                    ops.push(symbol);
                    i++;
                } else if (s.charAt(i) == ')') {
                    ops.pop();
                    i++;
                } else {
                    int num = 0;
                    while (i < size && Character.isDigit(s.charAt(i))) {
                        num = num * 10 + Character.getNumericValue(s.charAt(i));
                        i++;
                    }
                    ret += num * symbol;
                }

            }

            return ret;
        }


        /**
         * "4 + (1 +4 + (  5 + (7+5 ) + 2)-3)+(6+18) + 9";
         * 4 + (5    + (  5 +  12   + 2) -3) + 24  + 9
         *
         * @param s
         * @return
         */
        public int calculate02(String s) {
            Deque<Integer> ops = new LinkedList<Integer>();
            ops.push(1);
            int sign = 1;

            int ret = 0;
            int n = s.length();
            int i = 0;
            while (i < n) {
                if (s.charAt(i) == ' ') {
                    i++;
                } else if (s.charAt(i) == '+') {
                    sign = ops.peek();
                    i++;
                } else if (s.charAt(i) == '-') {
                    sign = -ops.peek();
                    i++;
                } else if (s.charAt(i) == '(') {
                    ops.push(sign);
                    i++;
                } else if (s.charAt(i) == ')') {
                    ops.pop();
                    i++;
                } else {
                    long num = 0;
                    while (i < n && Character.isDigit(s.charAt(i))) {
                        num = num * 10 + s.charAt(i) - '0';
                        i++;
                    }
                    ret += sign * num;
                }
            }
            return ret;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
